package moco.blatt9.soapwebservices;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	private TextView tv;
	private Button sync;
	
	private final String HOST = "10.0.2.2:5145"; 
	private final String WSDL_URL = "http://" + HOST + "/WebServiceLocalServerTomcat/services/Datum?wsdl";
	private final String NAMESPACE = "http://soap.blatt9.moco";
	private final String SOAP_ACTION = NAMESPACE + "/getDate";
    private final String METHOD_NAME = "getDate";
    

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		tv = (TextView) findViewById(R.id.tv);
		sync = (Button) findViewById(R.id.sync);
		sync.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				new AsyncTask<String, String, String>() {

					@Override
					protected String doInBackground(String... params) {
						String reponse = "#";
						SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
						request.addProperty("name", "JAaaaaaaaaaaaaaaaaa");
						
						SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
						envelope.setOutputSoapObject(request);
//						envelope.dotNet = true;
						
						try {
							HttpTransportSE httptransport = new HttpTransportSE(WSDL_URL);
							httptransport.call(SOAP_ACTION, envelope);
							
							if(envelope.bodyIn instanceof SoapFault){
								SoapFault sf = (SoapFault) envelope.bodyIn;
								reponse = sf.faultstring + ". " + sf.getMessage() + ". " + sf.getCause();
							} else {
								SoapObject response = (SoapObject) envelope.bodyIn;
								reponse = response.getProperty(0).toString();
							}
							
						} catch (IOException e) {
							System.out.println("Fehler: " + e.getMessage() + ". " + e.getCause());
						} catch (XmlPullParserException e) {
							System.out.println("Fehler: " + e.getMessage() + ". " + e.getCause());
						} catch (Exception e) {
							System.out.println("Fehler: " + e.getClass().getName() + ". " + e.getMessage() + ". " + e.getCause());
						}
						
						return reponse;
					}
					
					protected void onPostExecute(String result) {
						tv.setText(result);
					};
					
				}.execute("");
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId() == R.id.call_addition_activity) {
			Intent addition = new Intent();
			addition.setClassName(this, Addition.class.getName());
			startActivity(addition);
		} else if(item.getItemId() == R.id.call_tempr_activity){
			Intent cf = new Intent();
			cf.setClassName(this, CelsiusFahrenheit.class.getName());
			startActivity(cf);
		}
		return super.onOptionsItemSelected(item);
	}

}
